package com.wjb.xietong.ota.login;

import java.util.Calendar;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.wjb.xietong.R;
import com.wjb.xietong.component.AppGlobal;
import com.wjb.xietong.control.WJBControl;
import com.wjb.xietong.ota.BaseOTA;
import com.wjb.xietong.ota.OTAMainMenu;
import com.wjb.xietong.server.IRequestResultListener;
import com.wjb.xietong.server.login.LoginParam;
import com.wjb.xietong.util.PreferencesService;
import com.wjb.xietong.view.CleanableEditText;

public class OTALogin extends BaseOTA {

	private TextView tryButton;
	private Button loginButton;
	private CleanableEditText userName;
	private CleanableEditText userPwd;
	private long firstTime = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentField(getLayoutInflater().inflate(R.layout.login, null));
		hideTitleBar();
	}

	@Override
	protected void findView(View v) {
		super.findView(v);
		tryButton = (TextView) v.findViewById(R.id.try_button);
		loginButton = (Button) v.findViewById(R.id.login_button);
		userName = (CleanableEditText) v.findViewById(R.id.username_edit);
		userPwd = (CleanableEditText) v.findViewById(R.id.password_edit);

		PreferencesService preferences = new PreferencesService(AppGlobal
				.getInstance().getBaseContext());
		String name =  preferences
				.getLoginUserInfoPreference(PreferencesService.PREFERENCE_KEY_USER_NAME);
		String pwd = preferences
				.getLoginUserInfoPreference(PreferencesService.PREFERENCE_KEY_USER_NAME);
		if(null != name && null != pwd){
			userName.setText(""
					+ preferences
							.getLoginUserInfoPreference(PreferencesService.PREFERENCE_KEY_USER_NAME));
			userPwd.setText(""+preferences
					.getLoginUserInfoPreference(PreferencesService.PREFERENCE_KEY_USER_PASSWORD));
			login();
		}
	}

	@Override
	protected void setOnClickEvent() {
		super.setOnClickEvent();
		loginButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				login();
				
			}
		});
		
		tryButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				forward2Web();
			}
		});
	}
	
	//跳转至网页
	private void forward2Web(){
		Intent intent = new Intent( Intent.ACTION_VIEW );
		intent.setData(Uri.parse("http://www.wenjuan.com/s/YbAjIf")); //这里面是需要调转的url
		intent = Intent.createChooser(intent, null);
		startActivity(intent);
	}
	
	private void login(){
		LoginParam param = new LoginParam();
		param.setUserName(userName.getText().toString());
		param.setUserPwd(userPwd.getText().toString());
		param.setClientId(AppGlobal.getInstance().getClientId());
		
		if (null == param.getUserName()
				|| "".equals(param.getUserName())) {
			Toast.makeText(OTALogin.this, "请输入登录信息", Toast.LENGTH_LONG)
					.show();
			return;
		}
		showProgressDialog();
		
		WJBControl.requestLogin(Calendar.getInstance()
				.getTimeInMillis(), param,
				new IRequestResultListener() {

					@Override
					public void requestSuccess(long requestUniqueID) {
						hideProgressDialog();
						// 保存用户登录信息
						PreferencesService preferences = new PreferencesService(
								OTALogin.this);
						preferences.saveLoginUserInfo(userName
								.getText().toString(), userPwd
								.getText().toString(), AppGlobal
								.getInstance().getClientId());
						// 跳转至首页
						gotoMainMenu();
					}

					@Override
					public void requestFaield(long requestUniqueID,
							String errCode, String errMsg) {
						hideProgressDialog();
						Toast.makeText(OTALogin.this,
								errMsg == null ? "登录失败，请重新登录" : errMsg,
								Toast.LENGTH_LONG).show();
					}
				});
	}

	private void gotoMainMenu() {
		Intent intent = new Intent(OTALogin.this, OTAMainMenu.class);
		startActivity(intent);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			long secondTime = System.currentTimeMillis();
			if (secondTime - firstTime > 1500) {// 如果两次按键时间间隔大于1500毫秒，则不退出
				Toast.makeText(OTALogin.this,
						getString(R.string.STR_COMMON_11), Toast.LENGTH_SHORT)
						.show();
				firstTime = secondTime;// 更新firstTime
				return true;
			} else {
				app_.getActManager().popAllActivity();
			}
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}
}
